/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.surroundyou.DAOclasses;

import java.sql.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;
import com.surroundyou.dbResources.*;


/**
 *
 * @author pranay
 */
public class surroundYOUclassesImpl implements surroundYOUclassesInterface {
    private PreparedStatement pre_stmt;
    private Connection con;
    private static DbConPoolManager dbconnpool = new DbConPoolManager();

    public Document getUser(int uid) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Document getUsers() throws Exception {
        //Document doc=null;
        //Element zones=null;
        Element temp=null;
        Text val = null;
        String id;
        Document doc=null;
        Element usr;

        try{
            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
            doc = docBuilder.newDocument();
            Element root = doc.createElement("users");
            doc.appendChild(root);
            
            
            ResultSet rs;
            Connection con = dbconnpool.getConnection();
            int count=0;
            pre_stmt = con.prepareStatement("SELECT * FROM user");
            rs=pre_stmt.executeQuery();
            int numColumns=rs.getMetaData().getColumnCount();
            
            
            while (rs.next()){
                count++;
                //zones=null;
               usr=doc.createElement("user");
               root.appendChild(usr);
                
                for(int i=1;i<numColumns;i++){
                    temp=null;
                    val=null;
                    temp=doc.createElement(rs.getMetaData().getColumnName(i));
                    val=doc.createTextNode(String.valueOf(rs.getObject(i)));
                    usr.appendChild(temp);
                    temp.appendChild(val);
                }
            }
            if (count<=0)
            {
                System.out.println("No User Found");
            }
         }catch (Exception e){
                 e.printStackTrace();
                 
        }finally
    	{
    		dbconnpool.returnConnection(con);
    	}
        return doc;
    }

    public Document getFriends(int uid) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Document addFriend(int uid, int fuid) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Document deleteFriend(int uid, int fuid) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Document userLogin(int uid, String pwd) throws Exception {
        Element temp=null;
        Text val = null;
        String id;
        Document doc=null;
        Element usr;

        try{
            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
            doc = docBuilder.newDocument();
            Element root = doc.createElement("users");
            doc.appendChild(root);

            
            
            ResultSet rs;
            Connection con = dbconnpool.getConnection();
            int count=0;
            pre_stmt = con.prepareStatement("SELECT * FROM user where uid=? and pwd=?");
            pre_stmt.setInt(1, uid);
            pre_stmt.setString(2,pwd);
            rs=pre_stmt.executeQuery();
            int numColumns=rs.getMetaData().getColumnCount();
            
            
            while (rs.next()){
                count++;
                //zones=null;
               usr=doc.createElement("user");
               root.appendChild(usr);
                
                for(int i=1;i<numColumns;i++){
                    temp=null;
                    val=null;
                    temp=doc.createElement(rs.getMetaData().getColumnName(i));
                    val=doc.createTextNode(String.valueOf(rs.getObject(i)));
                    usr.appendChild(temp);
                    temp.appendChild(val);
                }
            }
            if (count<=0)
            {
                temp=null;
                //val=null;
                temp=doc.createElement("result");
                temp.setAttribute("status", "failure");
                root.appendChild(temp);

            }else{
                temp=null;
                //val=null;
                temp=doc.createElement("result");
                temp.setAttribute("status", "success");
                root.appendChild(temp);
                
            }
         }catch (Exception e){
                 e.printStackTrace();
                 
        }finally
    	{
    		dbconnpool.returnConnection(con);
    	}
        return doc;
    }

    public Document createUser(String email, String pwd, String fname, String lname, String address1, String address2, String city, String state, String country, int zip, String image_path) throws Exception {
        Document doc=null;
        Element zones=null;
        Element temp=null;
        Text val = null;
        
        
       try{
            con = dbconnpool.getConnection();
            pre_stmt=con.prepareStatement("insert into user(email,pwd,fname,lname,address1,address2,city,state,country,zip,image_path) values (?,?,?,?,?,?,?,?,?,?,?");
            pre_stmt.setString(1, email);
            pre_stmt.setString(2, pwd);
            pre_stmt.setString(3, fname);
            pre_stmt.setString(4, lname);
            pre_stmt.setString(5,address1 );
            pre_stmt.setString(6, address2);
            pre_stmt.setString(7, city);
            pre_stmt.setString(8, state);
            pre_stmt.setString(9, country);
            pre_stmt.setInt(10, zip);
            pre_stmt.setString(11, image_path);
            
            
            int ik=pre_stmt.executeUpdate();
            ResultSet rs = pre_stmt.getGeneratedKeys();
            int key=0;
            
            if (rs.next()) {
            key = rs.getInt(1);
           }
            
            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
            doc = docBuilder.newDocument();
        
            Element root = doc.createElement("users");
            doc.appendChild(root);
            Element result=doc.createElement("result");
            result.setAttribute("status", "success");
            doc.appendChild(root);
            Element userid=doc.createElement("uid");
            val=doc.createTextNode(String.valueOf(key));
            doc.appendChild(userid);
            userid.appendChild(val);
            
       }catch(Exception e){
           e.printStackTrace();
           DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
            doc = docBuilder.newDocument();
        
            Element root = doc.createElement("users");
            doc.appendChild(root);
            Element result=doc.createElement("result");
            result.setAttribute("status", "failure");
            doc.appendChild(root);
            Element userid=doc.createElement("errmsg");
            val=doc.createTextNode("Could not create user");
            doc.appendChild(userid);
            userid.appendChild(val);
       }finally{
           dbconnpool.returnConnection(con); 
           
       }
       return doc;
    }
    
     

}
